package com.ybkj.daijia.server.event.listener.zhuan;

import com.ybkj.daijia.PushDataV4;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiEmployV4;
import com.ybkj.daijia.api.jpush.JiGuangEmployV4;
import com.ybkj.daijia.server.ZhuanOrderTimerTask;
import com.ybkj.daijia.server.event.model.zhuan.ZhuanOrderCallBackEvent;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.scheduler.ThreadControl;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.zhuan.Order;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class ZhuanOrderCallBackEventListener implements
    ApplicationListener<ZhuanOrderCallBackEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhuanOrderCallBackEventListener.class);

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ZhuancheService zhuancheService;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @Override
    @Async
    public void onApplicationEvent(ZhuanOrderCallBackEvent event) {

        Order o = (Order) event.getSource();

        Order order = zhuancheService.findOne(o.getId());

        Driver driver = event.getDriver();

        try {

            //设备推送
            PushDataV4 pushDataV4 = new PushDataV4();
            pushDataV4.setCode("0X04");
            pushDataV4.setData(order.getId());
            pushDataV4.setType("0X01");
            Map<String, String> extra = new HashMap<String, String>();
            extra.put("content", "您的工单被收回了,单号:" + order.getOrderNo());
            pushDataV4.setExtra(extra);
            boolean getuiOnline = false;
            if (StringUtils.isNotBlank(driver.getUserID())) {
                getuiOnline = GeTuiEmployV4.getClientOnline(driver.getUserID(), settingUtils.get());
            }

            if (getuiOnline) {
                GeTuiEmployV4.push(driver, pushDataV4, settingUtils.get());
            } else {
                if (StringUtils.isNotBlank(driver.getRegistrationId())) {
                    JiGuangEmployV4.push(driver, pushDataV4, settingUtils.get());
                }
            }
            Setting setting = settingUtils.get();

			if (setting.isAutoPaidanZhuanche()) {  //专车订单自动派单定时器
	          Timer timer = new Timer();
	          ZhuanOrderTimerTask orderTimerTask=new ZhuanOrderTimerTask(
	                  setting.getAutoPaidanKm1(),setting.getAutoPaidanTime1(),
	                  setting.getAutoPaidanKm2(),setting.getAutoPaidanTime2(),
	                  setting.getAutoPaidanKm3(),setting.getAutoPaidanTime3(),
	                  applicationEventMulticaster,
	                  order.getId(),order.getStartLat(),order.getStartLng(),1
	          );
	          
	          Integer howLong=setting.getAutoPaidanTime1();
	          if(order.getServerTime().getTime()-System.currentTimeMillis()>1000*60*30){
	              howLong=(int) (order.getServerTime().getTime()-System.currentTimeMillis()-1000*60*30)/1000;
	          }
	          timer.schedule(orderTimerTask, howLong*1000);
	          ThreadControl.TaskMaps.put("task_"+order.getId(), timer);
			}
	          
		}catch(IOException e){
			logger.debug("push error : " + e.getMessage());
		}
		
	}

}
